package com.calsoft.Hops_Mobile_Tablet.base;
/*
 * User: Minh Diep
 * Date: 6/27/13
 * Time: 10:25 AM
 */

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.R;

import java.util.ArrayList;

public class CardUIAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<CardUIData> mData = new ArrayList<CardUIData>();
    private ArrayList<Integer> mType = new ArrayList<Integer>();
    private Typeface typeface;
    private Context context;

    public CardUIAdapter(Context context){
        this.context = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
    }

    public void addCardItem(CardUIData cardInfo){
        mData.add(cardInfo);
        mType.add(cardInfo.getType());
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mType.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return CardUI.TYPE_MAX_COUNT;
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public CardUIData getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView timeTextView;
        public TextView descTextView;
        public TextView nameTextView;
        public ImageView imageView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.card_ui_list_item, null);
            holder.nameTextView = (TextView) convertView.findViewById(R.id.card_ui_list_item_name);
            holder.descTextView = (TextView) convertView.findViewById(R.id.card_ui_list_item_desc);
            holder.imageView = (ImageView) convertView.findViewById(R.id.card_ui_list_item_image_view);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.card_ui_list_item_time);
            assert convertView != null;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        // Set up FONT for text view
        holder.nameTextView.setTypeface(typeface);
        holder.timeTextView.setTypeface(typeface);
        holder.descTextView.setTypeface(typeface);

        // Set up DATA
        if (mData.get(position).getType() == CardUI.TYPE_ADD_SERVICE){
            //Since the string is already formatted
            holder.timeTextView.setText(mData.get(position).getCardDesc());
            holder.descTextView.setText("");
        } else if (mData.get(position).getType() == CardUI.TYPE_HIS){
            holder.timeTextView.setText("HIS");
            holder.descTextView.setText(mData.get(position).getCardDesc());
        } else {
            holder.timeTextView.setText(mData.get(position).getStartTimeStrFormat());
            holder.descTextView.setText(mData.get(position).getCardDesc());
        }

        holder.nameTextView.setText(mData.get(position).getName());


        String imageName = mData.get(position).getImageTag();
        int drawableImageId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
        holder.imageView.setImageResource(drawableImageId);

        return convertView;
    }
}
